package com.atguigu.gmall.oauth.controller;

import com.atguigu.gmall.common.execption.GmallException;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.RegexValidateUtils;
import com.atguigu.gmall.oauth.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/***
 * 注册的控制层
 */
@RestController
@RequestMapping("/api/register")
public class RegisterController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 注册
     * @param userInfoMap
     * @return
     */
    @PostMapping
    public Result register(@RequestParam Map<String,String> userInfoMap){
        //获取用户的信息
        String username = userInfoMap.get("username");
        String password = userInfoMap.get("password");
        String phoneNum = userInfoMap.get("phoneNum");

        //1 参数校验
        if(userInfoMap.isEmpty()){
            throw new GmallException("参数错误",null);
        }
        if(StringUtils.isEmpty(username) ||
                StringUtils.isEmpty(password) ||
                StringUtils.isEmpty(phoneNum)){
            throw new GmallException("参数错误",null);
        }
        //判断参数是否非法
        if(RegexValidateUtils.checkCellphone(phoneNum)){
            throw new GmallException("手机号格式有误，请重新输入",null);
        }

        //2 调用业务层方法
        userInfoService.save(userInfoMap);

        //3 响应数据
        return Result.ok();
    }
}
